package com.tree.easycodedemo.service.impl;

import com.tree.easycodedemo.entity.City;
import com.tree.easycodedemo.dao.CityDao;
import com.tree.easycodedemo.service.CityService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 城市(City)表服务实现类
 *
 * @author makejava
 * @since 2022-04-17 21:36:24
 */
@Service("cityService")
public class CityServiceImpl implements CityService {
    @Resource
    private CityDao cityDao;

    /**
     * 通过ID查询单条数据
     *
     * @param cityId 主键
     * @return 实例对象
     */
    @Override
    public City queryById(Long cityId) {
        return this.cityDao.queryById(cityId);
    }

    /**
     * 分页查询
     *
     * @param city 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<City> queryByPage(City city, PageRequest pageRequest) {
        long total = this.cityDao.count(city);
        return new PageImpl<>(this.cityDao.queryAllByLimit(city, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param city 实例对象
     * @return 实例对象
     */
    @Override
    public City insert(City city) {
        this.cityDao.insert(city);
        return city;
    }

    /**
     * 修改数据
     *
     * @param city 实例对象
     * @return 实例对象
     */
    @Override
    public City update(City city) {
        this.cityDao.update(city);
        return this.queryById(city.getCityId());
    }

    /**
     * 通过主键删除数据
     *
     * @param cityId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long cityId) {
        return this.cityDao.deleteById(cityId) > 0;
    }
}
